package com.spring.boot.service.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot.entity.ChinaCity;
import com.spring.boot.entity.ProvinceCity;
import com.spring.boot.entity.Ranking;
import com.spring.boot.entity.UserInfo;
import com.spring.boot.mapper.ChinaCityMapper;
import com.spring.boot.response.Result;
import com.spring.boot.service.ChinaCityService;
import com.spring.boot.utils.CityId;
import com.spring.boot.utils.MyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

import static com.spring.boot.utils.MyUtils.*;

@Service
public class ChinaCityServiceImpl extends ServiceImpl<ChinaCityMapper, ChinaCity> implements ChinaCityService {

    @Override
    public void mySave(JSONArray chinaCaseList) {
        for (Object value : chinaCaseList) {
            JSONObject o = (JSONObject) value;
            if (o.get("area").equals("重庆")){
                ChinaCity city = o.toJavaObject(ChinaCity.class);
                city.setPro("中国");
                city.setCity("重庆");
                city.setCreateTime(new Date());
                city.setMark( city.getPro() + city.getCity()+getMarkDate(new Date()));
                super.saveOrUpdate(city);
            }
            else if (o.get("area").equals("北京")){
                ChinaCity city = o.toJavaObject(ChinaCity.class);
                city.setPro("中国");
                city.setCity("北京");
                city.setCreateTime(new Date());
                city.setMark( city.getPro() + city.getCity()+getMarkDate(new Date()));
                super.saveOrUpdate(city);
            }
            else if (o.get("area").equals("天津")){
                ChinaCity city = o.toJavaObject(ChinaCity.class);
                city.setPro("中国");
                city.setCity("天津");
                city.setCreateTime(new Date());
                city.setMark( city.getPro() + city.getCity()+getMarkDate(new Date()));
                super.saveOrUpdate(city);
            }
            else if (o.get("area").equals("上海")){
                ChinaCity city = o.toJavaObject(ChinaCity.class);
                city.setPro("中国");
                city.setCity("上海");
                city.setCreateTime(new Date());
                city.setMark( city.getPro() + city.getCity()+getMarkDate(new Date()));
                super.saveOrUpdate(city);
            }
            else if (o.get("area").equals("澳门")){
                ChinaCity city = o.toJavaObject(ChinaCity.class);
                city.setPro("中国");
                city.setCity("澳门");
                city.setCreateTime(new Date());
                city.setMark( city.getPro() + city.getCity()+getMarkDate(new Date()));
                super.saveOrUpdate(city);
            }
            else if (o.get("area").equals("台湾")){
                ChinaCity city = o.toJavaObject(ChinaCity.class);
                city.setPro("中国");
                city.setCity("台湾");
                city.setCreateTime(new Date());
                city.setMark( city.getPro() + city.getCity()+getMarkDate(new Date()));
                super.saveOrUpdate(city);
            }
            else if (o.get("area").equals("香港")){
                ChinaCity city = o.toJavaObject(ChinaCity.class);
                city.setPro("中国");
                city.setCity("香港");
                city.setCreateTime(new Date());
                city.setMark( city.getPro() + city.getCity()+getMarkDate(new Date()));
                super.saveOrUpdate(city);
            }
            else {
                JSONArray subList = (JSONArray) o.get("subList");
                List<ChinaCity> list = subList.toJavaList(ChinaCity.class);
                for (ChinaCity chinaCity : list) {
                    chinaCity.setPro(o.get("area").toString());
                    chinaCity.setCreateTime(new Date());
                    chinaCity.setMark(chinaCity.getPro() + chinaCity.getCity() + getMarkDate(new Date()));
                }
                super.saveOrUpdateBatch(list);
            }

        }
    }

    @Override
    public List<?> getRankingInfo(String date) {
        List<Ranking> result = new ArrayList<>();
        QueryWrapper<ChinaCity> chinaCityQueryWrapper = new QueryWrapper<>();
        if (!StrUtil.hasEmpty(date)) {
            chinaCityQueryWrapper.select("city", "confirmed_relative").eq("pro", "福建").likeRight("create_time", date);
        } else {
            chinaCityQueryWrapper.select("city", "confirmed_relative").eq("pro", "福建").likeRight("create_time", MyUtils.today());
        }
        List<ChinaCity> list = super.list(chinaCityQueryWrapper);
        for (ChinaCity city : list) {
            result.add(new Ranking(city.getCity(), Integer.parseInt(city.getConfirmedRelative())));
        }
        return result;
    }

    @Override
    public List<Integer> getProvinceMapData(String date) {
        List<Integer> integers = Arrays.asList(new Integer[9]);
        QueryWrapper<ChinaCity> chinaCityQueryWrapper = new QueryWrapper<>();
        chinaCityQueryWrapper.select("city", "cur_confirm")
                .eq("pro", "福建");
        if (StrUtil.hasEmpty(date)) {
            chinaCityQueryWrapper.likeRight("create_time", today());
        } else {
            chinaCityQueryWrapper.likeRight("create_time", date);
        }
        List<ChinaCity> list = super.list(chinaCityQueryWrapper);
        for (ChinaCity city : list) {
            switch (city.getCity()) {
                case ("福州"):
                    integers.set(0, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("厦门"):
                    integers.set(1, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("漳州"):
                    integers.set(2, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("泉州"):
                    integers.set(3, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("三明"):
                    integers.set(4, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("莆田"):
                    integers.set(5, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("南平"):
                    integers.set(6, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("龙岩"):
                    integers.set(7, Integer.valueOf(city.getCurConfirm()));
                    break;
                case ("宁德"):
                    integers.set(8, Integer.valueOf(city.getCurConfirm()));
                    break;
            }
        }
        return integers;

    }

    @Override
    public Map getCitiesTrend() {
        HashMap<String, List<?>> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        for (CityId cityId : CityId.values()) {
            QueryWrapper<ChinaCity> cityQueryWrapper = new QueryWrapper<>();
            cityQueryWrapper.eq("city", cityId.getCityName())
                    .between("create_time", "2022-03-01", MyUtils.getBeforeDays(MyUtils.today(), 1))
                    .orderByAsc("create_time");//根据城市名字查询
            List<ChinaCity> cityList = super.list(cityQueryWrapper);
            List<Integer> confirmList = new ArrayList<>();
            for (ChinaCity city : cityList) {
                confirmList.add(Integer.valueOf(city.getCurConfirm()));
                if (!dateList.contains(MyUtils.getMarkDate(city.getCreateTime()))) {
                    dateList.add(MyUtils.getMarkDate(city.getCreateTime()));
                }
            }
            map.put(cityId.getCityName(), confirmList);
        }
        map.put("date", dateList);
        return map;

    }

    @Override
    public Result importData(MultipartFile file) throws IOException {
        //可以导入非大数据量数据，过多会栈堆溢出
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        reader.addHeaderAlias("省份", "pro");
        reader.addHeaderAlias("城市", "city");
        reader.addHeaderAlias("本土新增", "nativeRelative");
        reader.addHeaderAlias("累计无症状", "asymptomatic");
        reader.addHeaderAlias("新增无症状", "asymptomaticRelative");
        reader.addHeaderAlias("累计确诊", "confirmed");
        reader.addHeaderAlias("新增确诊", "confirmedRelative");
        reader.addHeaderAlias("累计死亡", "died");
        reader.addHeaderAlias("新增死亡", "diedRelative");
        reader.addHeaderAlias("现有确诊", "curConfirm");
        reader.addHeaderAlias("累计治愈", "crued");
        reader.addHeaderAlias("新增治愈", "cruedRelative");
        reader.addHeaderAlias("城市代号", "cityCode");
        reader.addHeaderAlias("行政区编号", "administrativeCode");
//        reader.addHeaderAlias("唯一标识", "mark");
        reader.addHeaderAlias("更新时间", "updateTime");
        reader.addHeaderAlias("创建时间", "createTime");
        reader.addHeaderAlias("创建人", "createBy");
        reader.addHeaderAlias("更新人", "updateBy");
        List<ChinaCity> studentList = reader.readAll(ChinaCity.class);
//        System.out.println("ssssss=>"+studentList.toString());
        super.saveOrUpdateBatch(studentList);
        return Result.OK();
    }

    @Override
    public Object getChinaCityById(Integer id) {
        ChinaCity chinaCity = new ChinaCity(id);
        ChinaCity city = super.getOne(Wrappers.lambdaQuery(chinaCity));
        return city;
    }

    @Override
    public Object updateChinaCityById(ChinaCity dataChinaCity) {
        //关于ChinaCity的代码不合理，有id了再加个mark字段本就是多余的，还有其他表也是这样的
        //先删除再插入
        QueryWrapper<ChinaCity> cityQueryWrapper = new QueryWrapper<>();
        cityQueryWrapper.eq("id",dataChinaCity.getId());
        ChinaCity city = super.getOne(cityQueryWrapper);
        boolean remove = super.removeById(city.getMark());
        dataChinaCity.setMark(city.getMark());
        boolean save = super.save(dataChinaCity);
        return save;
    }

    @Override
    public Object removeChinaCityById(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",id);
        boolean remove = super.removeByMap(map);
        return remove;
    }


}
